package com.changanw.askforleavedemo.controller;

import com.changanw.askforleavedemo.model.AskForLeaveVO;
import com.changanw.askforleavedemo.model.RespBean;
import com.changanw.askforleavedemo.model.TaskVO;
import com.changanw.askforleavedemo.model.User;
import com.changanw.askforleavedemo.service.AskForLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
public class AskForLeaveController {

    @Autowired
    AskForLeaveService askForLeaveService;

    @PostMapping("/ask_for_leave")
    public RespBean askForLeave(@RequestBody AskForLeaveVO askForLeaveVO) {
        String approveUser = askForLeaveVO.getApproveUser();
        if (StringUtils.isEmpty(approveUser)){
            return RespBean.error("审批人不可以为空");
        }
        return askForLeaveService.askForLeave(askForLeaveVO);
    }

    @GetMapping("/users")
    public List<User> getAllUsers() {
        return askForLeaveService.getAllUsers();
    }
    @GetMapping("/getCurrentUser")
    public String getCurrentUser() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    @GetMapping("/unapprove")
    public List<AskForLeaveVO> getUnapproveProcess() {
        return askForLeaveService.getUnapproveProcess();
    }

    @GetMapping("/image/{processId}")
    public void processImage(@PathVariable String processId, HttpServletResponse response) throws IOException {
        askForLeaveService.processImage(processId, response);
    }

    @GetMapping("/tasks")
    public List<TaskVO> getCurrentUserAllTask() {
        return askForLeaveService.getCurrentUserAllTask();
    }

    @PostMapping("/approve")
    public RespBean approve(@RequestBody TaskVO taskVO) {
        return askForLeaveService.approve(taskVO);
    }


    @GetMapping("/history")
    public List<AskForLeaveVO> geHistoryProcess() {
        return askForLeaveService.geHistoryProcess();
    }

}